package com.l9lou.creeper.cupule.manipulator.support;

import com.l9lou.creeper.cupule.DataPool;
import com.l9lou.creeper.cupule.manipulator.DataCollector;
import com.l9lou.creeper.cupule.manipulator.DataTransferException;
import com.l9lou.creeper.cupule.manipulator.DataTransferer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * author: eric
 * Date: 11-7-26
 * Time: 下午1:49
 */
public class AppDataManipulator implements DataCollector{

    private DataPool dataPool = new DataPool();
    private DataPool notSentData = new DataPool();
    private DataTransferer transfer;
    private ExecutorService consumerDaemon = Executors.newSingleThreadExecutor();

    public void collect(List<Map<String, Object>> data) {
        this.dataPool.read(data);
        List<Map<String, Object>> transforedData = new ArrayList<Map<String, Object>>();
        consumerDaemon.execute();
    }


    public void setTransferer(DataTransferer transferer) {
        this.transfer = transferer;
    }

    private static class TransforTask implements Runnable{
        DataTransferer transferer;
        List<Map<String, Object>> data;
        public TransforTask(DataTransferer transferer, List<Map<String, Object>> data){
            this.transferer = transferer;
            this.data = new ArrayList<Map<String, Object>>(data);
        }

        public void run() {
            try{
                transferer.send(this.data);
            }catch (DataTransferException e){

            }
        }
    }
}
